using System;

namespace SGEngine.ProfilerEx
{
    public class UnitConvertUtility
    {
        public const float k_BytesToKB = 1024.0f;
        public const float k_BytesToMB = k_BytesToKB * 1024.0f;

        private const float k_MsToSecond = 1000.0f;
        
        public static string GetPrettySizeDesc(long bytes)
        {
            float totalDownloadSize = GetNearestUnitByBytes(bytes, out var totalDownloadSizeUnit);
            return totalDownloadSize + totalDownloadSizeUnit;
        }

        public static string GetPrettyTimeMsDesc(double time)
        {
            if (time > 1000)
            {
                return $"{(time / k_MsToSecond):F2}s";
            }
            return $"{time:F2}ms";
        }

        private static float GetNearestUnitByBytes(long bytes, out string unitString)
        {
            float result;
            if (bytes >= k_BytesToMB)
            {
                unitString = "MB";
                result = bytes / k_BytesToMB;
            }
            else if (bytes >= k_BytesToKB)
            {
                unitString = "kB";
                result = bytes / k_BytesToKB;
            }
            else
            {
                unitString = "B";
                result = bytes;
            }
            return (float)Math.Round(result, 2);
        }
        
        
        
    }
}